package microservice.demo.training21days.provider.service;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/*
 * 一个微服务可以有多个接口契约，这里使用RestSchema注解声明HelloService是一个契约id为hello的rest接口，同时会在启动时生成相应的契约，
 *
 * 这里的rest接口是使用spring MVC风格开发的，CSEJavaSDK支持的开发风格有REST（JAX-RS,SpringMVC）和RPC
 *
 * */
@RestSchema(schemaId = "hello")  //rest接口类，CSEJavaSDK会扫描这个类，生成契约
@RequestMapping(path = "/provider/v0")   //spring的注解
public class HelloService {

    @RequestMapping(path = "/hello/{name}",method = RequestMethod.GET)
    public String sayHello(@PathVariable(value = "name") String name){
        return "hello,"+name;
    }

    @PostMapping("/greeting")
    public GreetingResp greeting(@RequestBody Person person){

        GreetingResp greetingResp = new GreetingResp();

        if (Gender.MALE.equals(person.getGender())){
            greetingResp.setMsg("Hello,Mr."+person.getName());
        }else {
            greetingResp.setMsg("Hello,Ms."+person.getName());
        }
        greetingResp.setTime(new Date());
        return greetingResp;
    }
}
